package com.central.common.model

import com.central.common.enum.ResultCode

/**
 * @author  fly
 * @date  2021/5/25 15:15
 * @version 1.0
 */
data class CommonResult<T>(
    var code: Long? = null,
    var message: String? = null,
    var data: T? = null
)

fun <T> success(data: T) = CommonResult(ResultCode.SUCCESS.code, ResultCode.SUCCESS.msg, data)

fun <T> success(data: T, message: String) = CommonResult(ResultCode.SUCCESS.code, message, data)

fun <T> failed(resultCode: ResultCode) = CommonResult<T>(resultCode.code, resultCode.msg, null)

fun <T> failed(resultCode: ResultCode, message: String) = CommonResult<T>(resultCode.code, message, null)

fun <T> failed(message: String) = CommonResult<T>(ResultCode.FAILED.code, message, null)

fun <T> failed() = failed<T>(ResultCode.FAILED)


/**
 * 参数验证失败返回结果
 */
fun <T> validateFailed() = failed<T>(ResultCode.VALIDATE_FAILED)

/**
 * 参数验证失败返回结果
 */
fun <T> validateFailed(message: String) = failed<T>(ResultCode.VALIDATE_FAILED, message)

/**
 * 未登录返回结果
 */
fun <T> unauthorized(data: T) = CommonResult(ResultCode.UNAUTHORIZED.code, ResultCode.UNAUTHORIZED.msg, data)

/**
 * 未授权返回结果
 */
fun <T> forbidden(data: T) = CommonResult(ResultCode.FORBIDDEN.code, ResultCode.FORBIDDEN.msg, data)